package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.ContendMapper;
import com.csr.csrcheck.pojo.Contend;
import com.csr.csrcheck.service.ContendService;
import com.csr.csrcheck.service.ex.ApprovalsException;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ContendServiceImpl implements ContendService {
    @Resource
    private ContendMapper contendMapper;

    public List<Contend> getContendList() {
        List<Contend> list = this.contendMapper.getContendList();
        if (list == null)
            throw new ApprovalsException("");
        return list;
    }

    public PageResult getListpage(int pageNum, int pageSize, String company_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(pageNum, pageSize, company_name));
    }

    public int addcontend(Contend contend) {
        int row = this.contendMapper.addcontend(contend);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int updatecontend(Contend contend) {
        int row = this.contendMapper.updatecontend(contend);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public int deletecontend(int id) {
        int row = this.contendMapper.deletecontend(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Contend findcontendByid(int id) {
        return this.contendMapper.findcontendByid(id);
    }

    private PageInfo<Contend> getPageInfo(int pageNum, int pageSize, String company_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Contend> list = this.contendMapper.getContendListpage(company_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}